package ar.edu.itba.it.pdc.httpParser.models;

import java.util.LinkedList;
import java.util.List;

public class Token {
	List<Byte> name;
	TokenType type;

	public Token(List<Byte> name, TokenType type) {
		this.name = name;
		this.type = type;
	}
	
	public Token(String string){
		byte[] bytes = string.getBytes();
		name = new LinkedList<Byte>();
		for(byte b: bytes){
			name.add(b);
		}
		type = TokenType.WORD;
	}

	public boolean isType(TokenType type) {
		return type == this.type;
	}

	public boolean is(String str) {
		
		if (name == null) {
			return false;
		}
		
		if (str.length() != name.size()) {
			return false;
		}
		int i = 0;
		for (Byte c : name) {
			if (str.charAt(i++) != ((char) c.byteValue()))
				return false;
		}
		return true;
	}

	public boolean isNumber() {
		
		if (name == null) {
			return false;
		}
		
		for (Byte c : name) {
			if (!Character.isDigit((char) (byte) c))
				return false;
		}
		return true;
	}

	public String toString() {
		
		if (name == null) {
			if(type ==null){
				return null;
			}
			return type.name();
		}
		
		byte[] array = new byte[name.size()];
		int i = 0;
		for (Byte c : name) {
			array[i++] = c;
		}
		return new String(array);
	}

	public List<Byte> getName() {
		return name;
	}

	public int length() {
		return name == null ? 0 : name.size();
	}

	public Token firstSubToken(int bytesToRead) {
		return new Token(name.subList(0, bytesToRead), type);
	}
	
	public Token secondSubToken(int bytesToRead) {
		return new Token(name.subList(bytesToRead, name.size()), type);
	}
	
	public TokenType getType() {
		return type;
	}

	public void setName(List<Byte> linkedList) {
		name = linkedList;
	}
}
